/*
 * Copyright 2016 The Cartographer Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "slam_ros/node_options.h"

#include <vector>

#include "glog/logging.h"
#include "slam/common/configuration_file_resolver.h"
#include "slam/mapping/map_builder.h"
#include "slam_ros/node_constants.h"

namespace slam_ros {

NodeOptions CreateNodeOptions(
    ::slam::common::LuaParameterDictionary* const lua_parameter_dictionary) {
  NodeOptions options;
  options.map_builder_options = ::slam::mapping::CreateMapBuilderOptions(
      lua_parameter_dictionary->GetDictionary("map_builder").get());
  options.map_frame = lua_parameter_dictionary->GetString("map_frame");
  options.lookup_transform_timeout_sec =
      lua_parameter_dictionary->GetDouble("lookup_transform_timeout_sec");
  options.submap_publish_period_sec =
      lua_parameter_dictionary->GetDouble("submap_publish_period_sec");
  options.pose_publish_period_sec =
      lua_parameter_dictionary->GetDouble("pose_publish_period_sec");
  options.trajectory_publish_period_sec =
      lua_parameter_dictionary->GetDouble("trajectory_publish_period_sec");

  if (lua_parameter_dictionary->HasKey("cache_data_time_duration_sec")) {
    options.cache_data_time_duration_sec =
        lua_parameter_dictionary->GetDouble("cache_data_time_duration_sec");
  }

  if (lua_parameter_dictionary->HasKey("warning_log_path")) {
    options.warning_log_path =
        lua_parameter_dictionary->GetString("warning_log_path");
  }
  if (lua_parameter_dictionary->HasKey("max_warning_log_num")) {
    options.max_warning_log_num =
        lua_parameter_dictionary->GetInt("max_warning_log_num");
  }
  if (lua_parameter_dictionary->HasKey("sensor_data_log_path")) {
    options.sensor_data_log_path = std::string(getenv("HOME")) + "/" +
        lua_parameter_dictionary->GetString("sensor_data_log_path");
  }
  if (lua_parameter_dictionary->HasKey("max_bag_log_num")) {
    options.max_bag_log_num =
        lua_parameter_dictionary->GetInt("max_bag_log_num");
  }
  if (lua_parameter_dictionary->HasKey("clear_log_every_sec")) {
    options.clear_log_every_sec =
        lua_parameter_dictionary->GetInt("clear_log_every_sec");
  }

  if (lua_parameter_dictionary->HasKey("initial_pose_time_interval_sec")) {
    options.initial_pose_time_interval_sec =
        lua_parameter_dictionary->GetInt("initial_pose_time_interval_sec");
  }

  if (lua_parameter_dictionary->HasKey("min_reflector_fusion_num")) {
    options.min_reflector_fusion_num =
        lua_parameter_dictionary->GetInt("min_reflector_fusion_num");
  }

  return options;
}

std::tuple<NodeOptions, TrajectoryOptions> LoadOptions(
    const std::string& configuration_directory,
    const std::string& configuration_basename) {
  auto file_resolver =
      absl::make_unique<slam::common::ConfigurationFileResolver>(
          std::vector<std::string>{configuration_directory});
  const std::string code =
      file_resolver->GetFileContentOrDie(configuration_basename);
  slam::common::LuaParameterDictionary lua_parameter_dictionary(
      code, std::move(file_resolver));

  return std::make_tuple(CreateNodeOptions(&lua_parameter_dictionary),
                         CreateTrajectoryOptions(&lua_parameter_dictionary));
}

}  // namespace slam_ros
